<?php
	// load settings
	include_once('../settings.php');  
	set_include_path('..');
	
	session_start();
	
	
	$_REQUEST = array_merge($_GET, $_POST);
	try{
		// if is defined action, make it happen
		if(isset($_REQUEST['action'])){
			// make function name, if exists, call it
			$action = 'action_'.$_REQUEST['action'];
			if(isset($_REQUEST['controller'])){
				if (strpos($_REQUEST['controller'], '/') === false){
					$controller = $_REQUEST['controller'];
					$_REQUEST['controller'] = $_REQUEST['controller'].'/'.$_REQUEST['controller'];
				} else {
					$controller = explode('/',$_REQUEST['controller']);
					$controller = array_reverse($controller);
					$controller = $controller[0];
				}
				$filename = '../'.constant('TEMPLATE_DIR').$_REQUEST['controller'].'.php';
				if(file_exists($filename)){
					include_once($filename);
					if(isset($_REQUEST['action'])){
						$obj = new $controller();
						echo json_encode($obj->performActions($_REQUEST));
					}
				}	
			}
			else if(function_exists($action)){
				echo json_encode(call_user_func($action, $_REQUEST));
			}	
		}
	} catch (Exception $e){
		echo json_encode($e);
	}
?>